package org.example.interceptor;


import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang.time.DateFormatUtils;
import org.example.filter.ParameterRequestWrapper;
import org.example.util.IpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


/**
 * 全局拦截器 日志打点
 * @author hanxi
 */
public class GlobalInterceptor implements HandlerInterceptor {

    private static Logger logger = LoggerFactory.getLogger(GlobalInterceptor.class);


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.info("打点日志[{}]: URI:{},QUERY:{},IP:{}", DateFormatUtils.format(System.currentTimeMillis(),"yyyy-MM-dd HH:mm:ss,S"),
                request.getRequestURI(), ParameterRequestWrapper.getAllParams(request), IpUtils.getRealIp(request));
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) throws Exception {

    }
}
